import { FORM_PUBLIC_SETTING_KEY } from "@/views/formgen/components/GenerateForm/types/constants";
import { FormPublicSetting } from "@/views/formgen/components/GenerateForm/types/form";
import { inject, Ref } from "vue";

export function useFormPageHook() {
  // 每页一题的
  const formSetting = inject<Ref<FormPublicSetting> | null>(FORM_PUBLIC_SETTING_KEY, null);

  // 一页一题自动下一页
  const autoNextPage = (emits: any, currentPageNum: number, isRecoverFormData: boolean) => {
    const autoNext = formSetting?.value?.onePageOneQuestion?.autoNext || false;
    const enableOnePageOneQuestion = formSetting?.value?.onePageOneQuestion?.enableOnePageOneQuestion || false;
    // 恢复数据的情况下不自动下一页 不然在会乱跳页
    // 案例：随机抽提+每页一题+恢复上次提交
    if (enableOnePageOneQuestion && autoNext && !isRecoverFormData) {
      // 延迟一下 不然太快
      setTimeout(() => {
        emits("next", currentPageNum);
      }, 200);
    }
  };

  return {
    autoNextPage
  };
}
